#include "func.h"


int main(int argc, char const *argv[])
{
    
    DATA data;
    int sockfd;
    struct sockaddr_in serveraddr,clientaddr;
    int k;
    int n;
    if(argc<3)
	{
		fprintf(stderr,"Usage: %s<ip><port>\n",argv[0]);
		exit(1);
	}
	
    if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1)
	{
		perror("sockfd error");
		return -1;
	}
	//填充serveraddr结构体
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
	serveraddr.sin_port = htons(atoi(argv[2]));
	//建立链接
	if(connect(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0)
    {
        perror("fail to connect");
        return -1;
    }
    while (1)
    {
        title1();
        scanf("%d",&n);
        getchar();
        switch(n)
        {
        case 1:
            // bzero(&data,sizeof(DATA));
            log_in(sockfd,&data);
            text(&data);
            if(strcmp(data.text1,"y")==0)
            {
                goto NEXT1;
            }
            break;
        case 2:
            // bzero(&data,sizeof(DATA));
            regist(sockfd, &data);
            if(strcmp(data.text1,"y")==0)
            {
                goto NEXT1;
            }
            text(&data);
            break; 
        case 3:
            printf("退出成功\n");
            return 0;
        default:
            break;
        }


    }
NEXT1:
    while (1)
    {
        title2(&data);
        scanf("%d",&k);
        getchar();
        switch(k)
        {
            case 1:
                // bzero(&data,sizeof(DATA));
                ues_find(sockfd,&data);
                break;
            case 2:
                // bzero(&data,sizeof(DATA));
                history_find(sockfd,&data);
                break;
            case 3:
                // bzero(&data,sizeof(DATA));
                if(data.num == 0)
                {
                    change(sockfd,&data);
                    text(&data);
                }
                else
                {
                    change_sp(sockfd,&data);
                    text(&data);
                }
                break;
            case 4:
                // bzero(&data,sizeof(DATA));
                delete(sockfd,&data);
                    break;
            case 5:
                login_num = 2;
                log_out(sockfd,&data);
                   return 0;
            case 6:
                delete_history(sockfd,&data);
                break;
            case 7:
                printf("退出成功\n");
                goto EXIT;
            default:
                printf("您输入有误 请重新输入\n");
                break;
        }
    }

EXIT:  
    return 0;
}
